
    var div = document.querySelector(".newDivdiv")
    	// console.log(div)

    function newDiv(asd){
    	console.log(JSON.parse(asd.data))
    	var ses = JSON.parse(asd.data)
    	// var firstTime = document.querySelector(".firstTime")
    	// console.log($(".firstTime"))
    	if(ses.url != undefined){
    		if($(".firstTime").length != 0){
    			var years = ses.year
    			if($(".divDays").length == 0){
	    			var divDay = $("<div class='divDays'><p class='divDaysP'>"+ses.year+"</p><div class='divDaysN'></div</div>")
			    	$(divDay).appendTo(".firstTime")
			    	var divTimess = $("<div class='divTime'><div class='divTimeL'><div class='divTimeLT'><div class='divTimeImg'><img src='"+ses.url+"'></div><div class='divTimeName'><span class='divTimeNames'>"+ses.nickname+"</span><span class='divTimetime'>"+ses.time+"</span></div></div><div class='divTimeLB'>"+ses.nickname+"第"+ses.times+"次"+ses.content+"，立即沟通</div></div><div class='divTimeR' did='"+ses.id+"'><i class='iconfont icon-xiaoxi' id='tuandel1'></i></div></div>")
				    $(divTimess).appendTo(".divDaysN")
			    }else{
			    	if($(".divDaysP")[0].innerHTML == years){
	    				var divTimess2 = "<div class='divTime'><div class='divTimeL'><div class='divTimeLT'><div class='divTimeImg'><img src='"+ses.url+"'></div><div class='divTimeName'><span class='divTimeNames'>"+ses.nickname+"</span><span class='divTimetime'>"+ses.time+"</span></div></div><div class='divTimeLB'>"+ses.nickname+"第"+ses.times+"次"+ses.content+"，立即沟通</div></div><div class='divTimeR' did='"+ses.id+"'><i class='iconfont icon-xiaoxi' id='tuandel1'></i></div></div>"
			    		$($(".divTime")[0]).before(divTimess2)
			    	}else{
			    		var divDay2 = "<div class='divDays'><p class='divDaysP'>"+years+"</p><div class='divDaysN'></div</div>"
		    			$($(".divDays")[0]).before(divDay2)
		    			var divTimess = $("<div class='divTime'><div class='divTimeL'><div class='divTimeLT'><div class='divTimeImg'><img src='"+ses.url+"'></div><div class='divTimeName'><span class='divTimeNames'>"+ses.nickname+"</span><span class='divTimetime'>"+ses.time+"</span></div></div><div class='divTimeLB'>"+ses.nickname+"第"+ses.times+"次"+ses.content+"，立即沟通</div></div><div class='divTimeR' did='"+ses.id+"'><i class='iconfont icon-xiaoxi' id='tuandel1'></i></div></div>")
				    	$(divTimess).appendTo(".divDaysN")

			    	}
			    }
    		}
    		dinji();
			div.style.display="block"
			if($(".divTimers").length != 0){
				var divTimers = document.querySelector(".divTimers")
				divTimers.remove()
			}
			var divTimessT = $("<div class='divTimers'><div class='divTimeL'><div class='divTimeLT'><div class='divTimeImg'><img src='"+ses.url+"'></div><div class='divTimeName'><span class='divTimeNames'>"+ses.nickname+"</span><span class='divTimetime'>"+ses.time+"</span></div></div><div class='divTimeLB'>"+ses.nickname+"第"+ses.times+"次"+ses.content+"，立即沟通</div></div><div class='divTimeRR' did='"+ses.id+"'><i class='iconfont icon-xiaoxi' id='tuandel1'></i></div></div>")
		   	$(divTimessT).appendTo(".newDivdiv")
			div.style.animationName = 'yidongtop0';
			setTimeout(function(){
				div.style.animationName = 'yidongtop1';
				setTimeout(function(){
					var divTimers = document.querySelector(".divTimers")
					divTimers.remove()
					div.style.display="none"
				},1000)
			},7000)
			divTimessTclick()
    	}
    }
    function divTimessTclick(){
    	$(".divTimeRR").click(function(){
    		console.log(this.getAttribute("did"))
    	})
    }
    function dinji(){
    	if($(".newDivdiv").length != 0){
			var divTimeR = document.querySelectorAll(".divTimeR")
			Array.from(divTimeR).forEach(function(a,b){
				a.onclick = function(){
					console.log(a.getAttribute("did"))
				}
			})
		}
    }

    var token = document.cookie.slice(document.cookie.indexOf("=")+1)
	var lockReconnect = false;//避免重复连接
    var wsUrl = "wss://minitest.17jxyx.com/wss";
    var ws;
    var tt;
    function createWebSocket() {
      try {
        ws = new WebSocket(wsUrl);
        init();
      } catch(e) {
        console.log('catch');
        reconnect(wsUrl);
      }
    }
    function init() {
      ws.onclose = function () {
        console.log('链接关闭');
        reconnect(wsUrl);
      };
      ws.onerror = function() {
        console.log('发生异常了');
        reconnect(wsUrl);
      };
      ws.onopen = function (e) {
      	var login_data = '{"type":"login","client_name":"kk","token":"'+token+'"}';
        console.log("websocket握手成功，发送登录数据:"+login_data);
        ws.send(login_data);
        console.log(e)
        //心跳检测重置
        heartCheck.start();
      };
      ws.onmessage = function (event) {
        //拿到任何消息都说明当前连接是正常的
        console.log(event);
        newDiv(event)
        heartCheck.start();
      }
    }
    function reconnect(url) {
      if(lockReconnect) {
        return;
      };
      lockReconnect = true;
      //没连接上会一直重连，设置延迟避免请求过多
      tt && clearTimeout(tt);
      tt = setTimeout(function () {
        createWebSocket(url);
        lockReconnect = false;
      }, 4000);
    }
    //心跳检测
    var heartCheck = {
      timeout: 30000,
      timeoutObj: null,
      serverTimeoutObj: null,
      start: function(){
        console.log('start');
        var self = this;
        this.timeoutObj && clearTimeout(this.timeoutObj);
        this.serverTimeoutObj && clearTimeout(this.serverTimeoutObj);
        this.timeoutObj = setTimeout(function(){
          //这里发送一个心跳，后端收到后，返回一个心跳消息，
          console.log('心跳');
          ws.send('{"type":"pong"}');
          self.serverTimeoutObj = setTimeout(function() {
            console.log(111);
            console.log(ws);
            ws.close();
            // createWebSocket();
          }, self.timeout);

        }, this.timeout)
      }
    }
    createWebSocket(wsUrl);
